(function(){
	var pagenum=10;//每页数量
	var curpage=1;//当前页面
	var totalPage=0;//总页数
	//获取列表
	function getlist(curpage,pagenum){
	   var le_id=$("#le_id").val();
	   var or_id=$("#or_id").val();
	   var user_id=$("#user_id").val();
	   var leveling_name=$("#leveling_name").val();
	   var leveling_account=$("#leveling_account").val();
	   var keyword=$("#keyword").val();
	   var user_phone=$("#user_phone").val();
	   var startTime=$("#datepicker").val();
	   var endTime=$("#datepicker2").val();
	   $.ajax({
			// url:'http://web.icecn.net/comment/getCommentVoList.json',
			url:httpOrigin + '/comment/getCommentVoList.json',
			type:'POST',
			async:true,
			data:{
                offset:pagenum*(curpage-1),
				limit:pagenum,
				le_id:le_id,
				or_id:or_id,
				user_id:user_id,
				leveling_name:leveling_name,
				leveling_account:leveling_account,
				keyword:keyword,
				user_phone:user_phone,
				startTime:startTime,
				endTime:endTime,
			},
			xhrFields:{withCredentials:true},
			dataType: "json",
			success: function(data){
				if(data.code === 200 ){
				    dataset=data.t.result;
					if(dataset && dataset.length > 0){
					   var html = template('datalist',{dataset:dataset});
					   $("#example1").html(html);
					   totalPage=data.t.totalPage;
					   $(".pageinfo").html("当前"+curpage+"页/共"+totalPage+"页");
					   $(".curpage").val(curpage);
					   if(curpage==1){$(".prev").addClass("disabled")}else{$(".prev").removeClass("disabled")}
					   if(curpage==totalPage){$(".next").addClass("disabled")}else{$(".next").removeClass("disabled")}
					}else{
						$("#example1").html("<tbody><tr><td>查不到数据</td></tr></tbody>")
					};
				}
			}
	   });
	}
	var Page = {
		init:function(){
			this.render();
			this.event();
		},
		//绑定事件
		event:function(){
			//上一页
		    $(".prev").click(
			    function(){
					if(curpage>1){
				     	curpage=curpage-1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//下一页
		    $(".next").click(
			    function(){
					if(curpage<totalPage){
				     	curpage=curpage+1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//页码跳转
			$(".gopage").click(
			    function(){
					if($(".curpage").val()>=1&&$(".curpage").val()<=totalPage){
				    	curpage=parseInt($(".curpage").val());
					    getlist(curpage,pagenum)
					}else{
						$(".curpage").val(curpage);
					}
			    }
			)
			//搜索按钮
			$(".btnsearch").click(function(){
			  curpage=1;
			  getlist(curpage,pagenum)
			})
			//删除评论
			$(".content-wrapper").on("click", '.delete', function() {
				$("#modal-delete .modal-body").attr("data-id",$(this).attr("data-id"))
			})
			//确认删除评论
			$(".btndelete").click(function(){
			   var id=$("#modal-delete .modal-body").attr("data-id");
			   $.ajax({
					url:'http://web.icecn.net/comment/deleteComment.json',
					type:'POST',
					async:true,
					data:{
						id:id
					},
					dataType: "json",
					xhrFields:{withCredentials:true},
					success: function(data){
						if(data.code === 200 ){
							$("#modal-delete").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert("删除失败")	
						}
					}
			   });
		    })
			//显示隐藏评论
			$(".content-wrapper").on("click", '.shide', function() {
				$("#modal-hide .modal-body").attr("data-id",$(this).attr("data-id"))
				$("#modal-hide .modal-body").attr("data-is",$(this).attr("data-is"))
				$("#modal-hide .modal-body").html("确认"+$(this).html()+"?")
			})
			//确认显示隐藏评论
			$(".btnhide").click(function(){
			   var id=$("#modal-hide .modal-body").attr("data-id");
			   var is=$("#modal-hide .modal-body").attr("data-is");
			   if(is==1){
				   $.ajax({
						url:'http://web.icecn.net/comment/hideComment.json',
						type:'POST',
						async:true,
						data:{
							id:id
						},
						dataType: "json",
						xhrFields:{withCredentials:true},
						success: function(data){
							if(data.code === 200 ){
								$("#modal-hide").modal('hide');
								getlist(curpage,pagenum);
							}else{
								alert("隐藏失败")	
							}
						}
				   });
			   }else{
				   $.ajax({
						url:'http://web.icecn.net/comment/showComment.json',
						type:'POST',
						async:true,
						data:{
							id:id
						},
						dataType: "json",
					    xhrFields:{withCredentials:true},
						success: function(data){
							if(data.code === 200 ){
								$("#modal-hide").modal('hide');
								getlist(curpage,pagenum);
							}else{
								alert("显示失败")	
							}
						}
				   });
				 }
		    })
		},
		//渲染页面
		render:function(){
			getlist(curpage,pagenum)
		}
	};
	Page.init();
})();